public class CandidateKey {

	// This method acts as a uniqueness checker for the Candidate object based on the Email__c field
	public static void hasCandidateDuplicates(Candidate__c[] candidates){

		//TODO: Define a map for unique Candidates called "candidateMap" that maps a String to the Candidate
		Map<String, Candidate__c> candidateMap = new Map<String, Candidate__c>();

		//TODO: Declare a for loop to cycle through the candidates.
		for( Candidate__c candidate:candidates ){

			// TODO: Within that loop, check to verify that the email is not null
			if( candidate.email__c != null){

				if( candidateMap.containsKey(candidate.unique_key__c) ){
					// and if it has a value pull back the unique_key__c
					// and then check to see if there are duplicate unique keys within this batch of records.  If there are, use addError to prevent them,
					// candidate.addError('Duplicate LastName + Email found in batch');
					candidate.addError('バッチ処理にて姓と電子メールでの重複を発見しました。');
				}
				else {
					// and if not, add the unique key/candidate combo to the candidateMap.
					candidateMap.put(candidate.unique_key__c, candidate);
				}
			}
		}

  		//Now hit the DB looking for dupes
  		if (!candidateMap.isEmpty()){
	  		for(Candidate__c[] candidatesCheck:[select unique_key__c from Candidate__c where unique_key__c IN :candidateMap.keySet()]) {
				for(Candidate__c candidate:candidatesCheck) {
	  				String currentCandidateId = candidate.Id;
	  				//if there's a dupe and this isn't the current record, then add the error.
	  				if(candidateMap.containsKey(candidate.unique_key__c) &&
	  					(currentCandidateId != candidateMap.get(candidate.unique_key__c).Id)) {
	     				// candidateMap.get(candidate.unique_key__c).addError('Duplicate LastName + Email found in salesforce(id: ' + candidate.id + ')');
	     				candidateMap.get(candidate.unique_key__c).addError('姓と電子メールでの重複を発見しました。salesforce(id: ' + candidate.id + ')');
	  				}
	  			}
			}
		}
	}
}